package com.business.config;

import com.business.ZkClient;
import com.business.controller.IndexController;
import com.business.db.DerbyConn;
import com.jfinal.config.*;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.util.TouchJdbc;
import org.apache.commons.dbutils.DbUtils;
import org.slf4j.LoggerFactory;

import java.sql.Connection;


/**
 * Created by xuanyao on 16-1-27.
 */
public class MyConfig extends JFinalConfig {

	private static org.slf4j.Logger logger = LoggerFactory.getLogger(MyConfig.class);
	public static DruidPlugin dp = null;
	public static ActiveRecordPlugin arp = null;

	@Override
	public void configConstant(Constants me) {
		me.setBaseViewPath("/WEB-INF/jsp");
		me.setViewType(ViewType.JSP);
		me.setUrlParaSeparator("-");
		PropKit.use("jdbc.properties");
	}

	@Override
	public void configRoute(Routes routes) {
		routes.add("/", IndexController.class,"/index");
	}

	/*@Override
	public void configEngine(Engine engine) {

	}*/


	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {

	}

	@Override
	public void configInterceptor(Interceptors interceptors) {

	}

	@Override
	public void configHandler(Handlers handlers) {

	}

	@Override
	public void afterJFinalStart() {
		TouchJdbc touchJdbc = TouchJdbc.getInstance();
		Connection conn = DerbyConn.getConn();
		try {
			String sql = "create table server_info(uuid varchar(64), ip varchar(16),port int,create_time TIMESTAMP,cluster_id  varchar(64))";
			touchJdbc.update(conn,sql);
			logger.info("创建数据库表 ："+sql);
		}catch (Exception e){

		}
		try {
			String sql = "create table cluster_info(uuid varchar(64), name varchar(64),create_time TIMESTAMP)";
			touchJdbc.update(conn,sql);
			logger.info("创建数据库表 ："+sql);
		}catch (Exception e){

		}
		logger.info("项目启动……");
	}

	@Override
	public void beforeJFinalStop() {
		//关闭数据库连接
		DbUtils.closeQuietly(DerbyConn.getConn());
		//关闭 zk 客户端
		ZkClient.close();
		logger.info("项目停止……");
	}

	public static void main(String[] args) {

	}
}
